<?php

/**
 * Description of G_Sqlite
 *
 * @author kouhongfei
 */
if (function_exists('sqlite_open') === FALSE) {
    trigger_error('系統不支持Sqlite');
    die();
}

class G_Sqlite extends GC_Db {

    protected $_sqlite;

    public function __construct($conf) {
        if (isset($conf['path'])) {
            $this->_sqlite = sqlite_open($conf['path']);
            if ($this->_sqlite) {
                
            } else {
                trigger_error('连接数据库失败');
                die();
            }
        }
    }

    public function affectedRows() {
        return sqlite_changes($this->_sqlite);
    }

    public function begin() {
        return $this->query("begin");
    }

    public function commit() {
        return $this->query("commit");
    }

    public function escape($str) {
        return sqlite_escape_string($str);
    }

    public function exec($sql) {
        return sqlite_exec($this->_sqlite, $sql);
    }

    public function getLastId() {
        return sqlite_last_insert_rowid($this->_sqlite);
    }

    public function getResults($sql) {
        $res = $this->query($sql);
        if ($res) {
            return sqlite_fetch_all($res, SQLITE_ASSOC);
        }
        return null;
    }

    public function getRow($sql) {
        $res = $this->query($sql);
        if ($res) {
            return sqlite_fetch_array($res, SQLITE_ASSOC);
        }
        return null;
    }

    public function getVar($sql) {
        $row = $this->getRow($sql);
        if ($row) {
            foreach ($row AS $v) {
                return $v;
            }
        }
        return null;
    }

    public function query($sql) {
        return sqlite_query($this->_sqlite, $sql);
    }

    public function rollback() {
        return $this->query("rollback");
    }

}

?>
